package com.jeect.userservice3.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jeect.userservice3.util.ResponseBody;
import com.jeect.userservice3.util.ResponseCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@Component
public class AuthLogoutSuccessHandler implements LogoutSuccessHandler {
    @Autowired
    private ObjectMapper objectMapper;


    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
//        authentication.setAuthenticated(false);
//        SecurityContextHolder.getContext().getAuthentication();
        System.out.println("onlogoutSuccess()");
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter printWriter = response.getWriter();
        authentication.setAuthenticated(false);
        ResponseBody responseBody = new ResponseBody(ResponseCode.SUCCESS,"登出成功!");
        printWriter.write(objectMapper.writeValueAsString(responseBody));
    }
}
